package wec_wallet.ebo.com.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import wec_wallet.ebo.com.R;
import wec_wallet.ebo.com.bean.Address;

/**
 * Created by Administrator on 2018/3/27.
 */

public class AddressAdapter extends ArrayAdapter<Address> {
    List<Address> mStringArray = new ArrayList<>();
    private Context mContext;

    public AddressAdapter(Context context, List<Address> stringArray) {
        super(context,android.R.layout.simple_spinner_item, stringArray);
        mContext = context;
        mStringArray = stringArray;
    }


    @Override
    public View getDropDownView(final int position, View convertView, ViewGroup parent) {
// 修改Spinner下拉字体颜色
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.item_address, parent, false);
        }
        TextView name = convertView.findViewById(R.id.item_name);
        TextView content = convertView.findViewById(R.id.item_content);
        View item_add = convertView.findViewById(R.id.item_add);

        if (position == mStringArray.size() - 1) {
            name.setVisibility(View.GONE);
            content.setVisibility(View.GONE);
            item_add.setVisibility(View.VISIBLE);
            item_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemOnclickListener.onClick(view,position);

                }
            });
        } else {
            name.setVisibility(View.VISIBLE);
            content.setVisibility(View.VISIBLE);
            item_add.setVisibility(View.GONE);
            name.setText(mStringArray.get(position).getName());
            content.setText(mStringArray.get(position).getAddress());
        }

        return convertView;
    }

    public interface OnItemOnclickListener{
        void onClick(View view,int postion);
    }
    public void setOnItemClickLitener(OnItemOnclickListener mOnItemClickLitener) {
        this.onItemOnclickListener = mOnItemClickLitener;
    }
    OnItemOnclickListener onItemOnclickListener;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
// 修改Spinner显示字体颜色
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.spinner_display_style1, parent, false);
        }

//注意：text1是系统默认Spinner用来显示文字的TextView控件名称，不可以修改
        TextView txtvwDisplay = (TextView) convertView.findViewById(R.id.txtvwSpinner);
        txtvwDisplay.setText(mStringArray.get(position).getName());
        return convertView;
    }

}
